﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.CorporationModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using FluentValidation;
    using Mulala.Cavan.Core.Helper;
    using Mulala.Cavan.Domain.MainBoundedContext.Resources;
    using FluentValidation.Validators;
    using System.Text.RegularExpressions;
    using Mulala.Cavan.Core.Extension;
    using FluentValidation.Results;

    public class CorporationValidator : AbstractValidator<Corporation>
    {
        public CorporationValidator()
        {
            RuleFor(c => c.FullName).NotEmpty().WithMessage(Messages.Validate_CorporationFullNameIsInvalid).Length(2, 50).WithMessage(Messages.Validate_CorporationFullNameIsInvalid);
            RuleFor(c => c.UserAccount).NotNull();
            RuleFor(c => c.Area).NotNull();
            RuleFor(c => c.RegistrationTime).NotNull().WithMessage(Messages.Validate_CorporationRegistrationTimeIsInvalid).LessThan(DateTime.Now).WithMessage(Messages.Validate_CorporationRegistrationTimeIsInvalid);
            RuleFor(c => c.Contact).NotEmpty().WithMessage(Messages.Validate_CorporationContactIsInvalid);
            RuleFor(c => c.ContactEmail).EmailAddress().WithMessage(Messages.Validate_EmailIsInvalid);
            RuleFor(c => c.Address).Length(2, 50).WithMessage(Messages.Validate_AddressIsInvalid);
            RuleFor(c => c.Telephone).Matches(StringHelper.TELEPHONEREGULAR).WithMessage(Messages.Validate_TelephoneIsInvalid);
            RuleFor(c => c.Mobile).Matches(StringHelper.TELEPHONEREGULAR).WithMessage(Messages.Validate_TelephoneIsInvalid);
            RuleFor(c => c.Introduce).Length(2, 2000).WithMessage(Messages.Validate_CorporationIntroduceIsInvalid);
            RuleFor(c => c.Culture).Length(2, 2000).WithMessage(Messages.Validate_CorporationCultureIsInvalid);
            RuleFor(c => c.Capital).Length(2, 50).WithMessage(Messages.Validate_CorporationCapitalIsInvalid);
            RuleFor(c => c.ChargeDescription).Length(2, 2000).WithMessage(Messages.Validate_CorporationChargeDescriptionIsInvalid);
            RuleFor(c => c.HomePage).Matches(StringHelper.URLREGULAR).WithMessage(Messages.Validate_CorporationHomePageIsInvalid);
            RuleFor(c => c.BusinessLicence).NotEmpty().WithMessage(Messages.Validate_CorporationBussinessLicenseIsInvalid);
            RuleFor(c => c.CustomService).Length(2, 100).WithMessage(Messages.Validate_CorporationCustomServiceIsInvalid);
            RuleFor(c => c.CorporationStatus).NotNull();
            RuleFor(c => c.Scale).GreaterThan(0).WithMessage(Messages.Validate_CorporationScaleIsInvalid);
            RuleFor(c => c.Speciality).GreaterThan(0).WithMessage(Messages.Validate_CorporationSpecialityIsInvalid);
            RuleFor(c => (int)c.Type).GreaterThan(0).WithMessage(Messages.Validate_CorporationTypeIsInvalid);
        }

        public override ValidationResult Validate(Corporation instance)
        {
            ValidationResult result = base.Validate(instance);
             
            if (instance.Telephone.IsEmpty() && instance.Mobile.IsEmpty())
            {
                result.Errors.Add(new ValidationFailure("Telephone", Messages.Validate_TelephoneMobileIsInvalid));
            }
            if (instance.RegistrationTime > DateTime.Now)
            {
                result.Errors.Add(new ValidationFailure("RegistrationTime", Messages.Validate_CorporationRegistrationTimeIsInvalid));
            }
            return result;
        }
    }

    //private class CorporationTelephonMobileValidator<T> : PropertyValidator
    //{
    //    public CorporationTelephonMobileValidator()
    //        : base(Messages.Validate_TelephoneMobileIsInvalid)
    //    {

    //    }

    //    protected override bool IsValid(PropertyValidatorContext context)
    //    {
    //        var corporation = (Corporation)context.PropertyValue;

    //        if (corporation.Telephone.IsEmpty() && corporation.Mobile.IsEmpty())
    //        {
    //            return false;
    //        }
    //        return true;
    //    }
    //}
}
